package com.start.workflow.core.listener;

import com.start.common.std.StartBaseSingleton;
import com.start.common.std.StartSingletonFactory;
import com.start.common.tools.SpringContextTools;
import com.start.form.engine.core.StartFormEngine;
import com.start.form.engine.core.model.StartFormConfigModel;
import com.start.workflow.common.enums.StartActHandlerStatus;
import com.start.workflow.common.enums.StartActUserTaskConf;
import com.start.workflow.core.dispatcher.StartGlobalActInstEventDispatcher;
import com.start.workflow.core.manage.StartActFormManager;
import com.start.workflow.core.service.StartActBusinessService;
import com.start.workflow.core.service.StartActModelConfigService;
import com.start.workflow.entity.StartActBusinessEntity;
import com.start.workflow.entity.StartActModelConfig;
import io.swagger.v3.oas.annotations.media.Schema;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.impl.ActivitiProcessStartedEventImpl;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.impl.persistence.entity.ExecutionEntityImpl;
import org.activiti.engine.repository.ProcessDefinition;

import java.time.LocalDateTime;

 @Schema(name="流程结束事件")
public class StartGlobalActProcessHandler extends StartBaseSingleton<StartGlobalActProcessHandler> {

    private static StartGlobalActProcessHandler instance;

    private HistoryService historyService;

    private StartFormEngine startFormEngine;

    private StartActBusinessService startActBusinessService;

    private StartActModelConfigService startActModelConfigService;


    private RepositoryService repositoryService;

    private StartActFormManager actFormLoadManager;


    public static StartGlobalActProcessHandler getInstance() {
        return StartSingletonFactory.get(StartGlobalActProcessHandler.class);
    }

    /**
     * 调用初始化函数
     */
    @Override
    public void init() {
        historyService = SpringContextTools.getBean(HistoryService.class);
        startFormEngine = SpringContextTools.getBean(StartFormEngine.class);
        startActBusinessService = SpringContextTools.getBean(StartActBusinessService.class);
        startActModelConfigService = SpringContextTools.getBean(StartActModelConfigService.class);
        repositoryService = SpringContextTools.getBean(RepositoryService.class);
        actFormLoadManager = StartActFormManager.getInstance();

    }

    /**
     * 流程审批通过执行
     *
     * @param event
     */
    public void processCompleted(ActivitiEvent event) {
        String instanceId = event.getProcessInstanceId();
        HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(instanceId).singleResult();
        StartActBusinessEntity startActBusinessEntity = startActBusinessService.getOneByProcessInstanceId(instanceId);
        if (startActBusinessEntity == null) return;
        StartGlobalActInstEventDispatcher.getInstance().onActInstanceCompleteBefore(startActBusinessEntity);
        startActBusinessEntity.setProcHandlerState(StartActHandlerStatus.agree.getValue());
        startActBusinessEntity.setProcEndTime(LocalDateTime.now());
        startActBusinessService.updateById(startActBusinessEntity);
        StartFormConfigModel form = startFormEngine.getForm(startActBusinessEntity.getProcBusFormId());
        if (null != form) {
            StartActModelConfig actModelConfig = startActModelConfigService.getActModelConfig(historicProcessInstance.getDeploymentId(), historicProcessInstance.getProcessDefinitionKey(), StartActUserTaskConf.FORM_TABLE);
            if (null != actModelConfig) {
                actFormLoadManager.onActStateChange(actModelConfig, StartActHandlerStatus.agree, instanceId);
            }
        }
        StartGlobalActInstEventDispatcher.getInstance().onActInstanceCompleteAfter(startActBusinessEntity);
    }


}
